
package com.ido.babyshow.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.ido.babyshow.common.GlobalConstant;
import com.ido.babyshow.model.Mother;
import com.ido.babyshow.model.Photo;
import com.ido.babyshow.response.BaseResponse;
import com.ido.babyshow.service.IMotherService;
import com.ido.babyshow.util.JsonUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Servlet implementation class ShowPicAction
 */
public class MotherAction extends ActionSupport
{

    private final static Logger logger = Logger.getLogger(MotherAction.class);

    private Mother mother;
    
    private Photo photo;
    
    // 上传文件集合
    private File[] uploadImages; 
    // 上传文件名集合
    private String[] uploadImagesFileName;
    // 上传文件内容类型
    private String[] uploadImagesContentType;    
    
    
    public Photo getPhoto()
    {
        return photo;
    }
    

    public void setPhoto(Photo photo)
    {
        this.photo = photo;
    }


    public Mother getMother()
    {
        return mother;
    }

    public void setMotherService(IMotherService motherService)
    {
        this.motherService = motherService;
    }    

    public File[] getUploadImages()
    {
        return uploadImages;
    }

    public void setUploadImages(File[] uploadImages)
    {
        this.uploadImages = uploadImages;
    }

    public String[] getUploadImagesFileName()
    {
        return uploadImagesFileName;
    }

    public void setUploadImagesFileName(String[] uploadImagesFileName)
    {
        this.uploadImagesFileName = uploadImagesFileName;
    }

    public String[] getUploadImagesContentType()
    {
        return uploadImagesContentType;
    }

    public void setUploadImagesContentType(String[] uploadImagesContentType)
    {
        this.uploadImagesContentType = uploadImagesContentType;
    }


    @Resource
    private IMotherService motherService;

    public String toEdit()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(false);
        mother = (Mother)session.getAttribute(GlobalConstant.MOTHER_SESSION);
        motherService.addPro(mother);
        return SUCCESS;
    }
    

    public String toView()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(false);
        mother = (Mother)session.getAttribute(GlobalConstant.MOTHER_SESSION);
        motherService.addPro(mother);
        return SUCCESS;
    }

    public void publishMessage()
    {
        try
        {
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpServletResponse response = ServletActionContext.getResponse();
            response.setContentType("application/json;charset=UTF-8");
            
            BaseResponse result = new BaseResponse();
            HttpSession session = request.getSession(false);
            mother =
                (Mother)session.getAttribute(GlobalConstant.MOTHER_SESSION);
            try
            {
                if (mother == null)
                {
                    response.sendRedirect(GlobalConstant.HOME);
                }
            }
            catch (IOException ex)
            {
                logger.error("用户发布微博转向到首页出错", ex);
            }
            String message = request.getParameter("message");
            String email = mother.getEmail();
            if (StringUtils.isBlank(message))
            {
                result.setMessage("内容不能为空!");
            }
            else
            {
                boolean opt = motherService.publishMessage(email, message);
                if (!opt)
                {
                    result.setMessage("数据库插入失败!");
                }
                else
                {
                    result.setFlag(true);
                }
            }
            PrintWriter out = response.getWriter();
            JsonUtil.writeJson(out, result);

        }
        catch (Exception ex)
        {
            logger.error("用户发布微博信息失败", ex);
        }
    }
    
    //用户上传分享图片
    public void shareImage()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("application/json;charset=UTF-8");
        BaseResponse result = new BaseResponse();
        try
        {
         
         
            HttpSession session = request.getSession(false);
            mother =
                (Mother)session.getAttribute(GlobalConstant.MOTHER_SESSION);
            try
            {
                if (mother == null)
                {
                    response.sendRedirect(GlobalConstant.HOME);
                }
            }
            catch (IOException ex)
            {
                logger.error("用户发布微博转向到首页出错", ex);
            }
           
            if (uploadImages==null||uploadImages.length==0)
            {
                result.setMessage("上传内容不能为空!");
            }
            else
            {
                String fileType=uploadImagesFileName[0].substring(uploadImagesFileName[0].lastIndexOf("."));
                boolean opt = motherService.shareImage(photo,uploadImages[0],fileType, mother);
                if (!opt)
                {
                    result.setMessage("数据库插入失败!");
                }
                else
                {
                    result.setFlag(true);
                }
            }
        }
        catch (Exception ex)
        {
            logger.error("用户分享图片失败", ex);
            result.setMessage("系统错误,请稍后重试!");
        }
            PrintWriter out;
            try
            {
                out = response.getWriter();
                JsonUtil.writeJson(out, result);
            }
            catch (IOException e)
            {
               
            }
           

       
    }
}
